package com.njitt.bysj.srevice;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.*;
import com.njitt.bysj.mapper.*;
import com.njitt.bysj.query.OutMaterialQuery;
import com.njitt.bysj.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/21 10:47
 */
@Service
public class ProductionPickService extends BaseService<ProductionPickBean, Integer> {

    @Resource
    private ProductionPickBeanMapper pickBeanMapper;
    @Resource
    private ProjectDesignService designService;
    @Resource
    private ProjectService projectService;
    @Resource
    private WarehouseBeanMapper warehouseBeanMapper;
    @Resource
    private BomBeanMapper bomBeanMapper;
    @Resource
    private MaterialBeanMapper materialBeanMapper;
    @Resource
    private ProjectBeanMapper projectBeanMapper;
    @Resource
    private ProjectQualityBeanMapper qualityBeanMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    public void pickMaterial(ProductionPickBean productionPickBean) {
        /**
         * @description: 正常领料
         * @return:  * @param productionPickBean
         * @author:
         * @time: 2022/4/21 10:48
         */
        WarehouseBean warehouseBean = warehouseBeanMapper.selectByMaterialNameAndMaterialNo(productionPickBean.getMaterialName(), productionPickBean.getMaterialNo());
        AssertUtil.isTrue(productionPickBean.getProjectId() == null, "关联项目不能为空");
        ProjectBean projectBean = projectService.selectProjectByProjectId(productionPickBean.getProjectId());
        AssertUtil.isTrue(projectBean == null || StringUtils.isBlank(projectBean.getProjectNo()), "项目不存在");
        AssertUtil.isTrue(StringUtils.isBlank(productionPickBean.getMaterialNo()), "物料编码不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(productionPickBean.getMaterialName()), "物料名称不能为空");
        AssertUtil.isTrue(productionPickBean.getMaterialCount() == 0 || productionPickBean.getMaterialCount() == null, "物料数量不能为空");
        Integer designCount = designService.selectBomByProjectNoAndMaterialNo(projectBean.getProjectNo(), productionPickBean.getMaterialNo());
        System.out.println(designCount + "designCount");
        Integer pastPickTotal = pickBeanMapper.pastPickTotal(projectBean.getId(), productionPickBean.getMaterialNo());
        System.out.println("pickCount" + productionPickBean.getMaterialCount());
        AssertUtil.isTrue(warehouseBean.getMaterialCount() < productionPickBean.getMaterialCount(), "库存不足");
        AssertUtil.isTrue(designCount < productionPickBean.getMaterialCount(), "调整输入数量符合bom");
        AssertUtil.isTrue(pastPickTotal + productionPickBean.getMaterialCount() > designCount, "调整领料数量符合bom,填写补零单");
        productionPickBean.setPickType(0);
        productionPickBean.setStatus(0);

        Integer integer = pickBeanMapper.insertSelective(productionPickBean);
        AssertUtil.isTrue(integer == null || integer == 0, "领料失败");


    }

    public Map<String, Object> selectOutMaterial(OutMaterialQuery query) {
        Map<String, Object> map = new HashMap<>(16);
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<ProductionPickBean> info = new PageInfo<>(pickBeanMapper.selectOutMaterial(query));
        map.put("data", info.getList());
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    public void outUpdateWarehouse(Integer id) {
        /**
         * @description: 更新库存
         * @return:  * @param productionPickBean
         * @author:
         * @time: 2022/4/21 16:17
         */
        // 更新库存
        System.out.println(id);
        AssertUtil.isTrue(id == null || id == 0, "发料失败");
        ProductionPickBean productionPickBean = pickBeanMapper.selectByPrimaryKey(id);

        WarehouseBean warehouseBean = warehouseBeanMapper.selectByMaterialNameAndMaterialNo(productionPickBean.getMaterialName(), productionPickBean.getMaterialNo());
        AssertUtil.isTrue(warehouseBean == null, "领料失败");
        AssertUtil.isTrue(warehouseBean.getMaterialCount() < productionPickBean.getMaterialCount(), "库存不足");
        warehouseBean.setMaterialCount(warehouseBean.getMaterialCount() - productionPickBean.getMaterialCount());
        productionPickBean.setPicktime(new Date());
        productionPickBean.setPickNo("" + System.currentTimeMillis());
        Integer integer = pickBeanMapper.updateByPrimaryKeySelective(productionPickBean);
        AssertUtil.isTrue(integer == 0 || integer == null, "发料失败");

        Integer integer1 = warehouseBeanMapper.updateByPrimaryKeySelective(warehouseBean);
        AssertUtil.isTrue(integer1 == 0 || integer1 == null, "库存更新失败");
    }

    public void addSupplementMaterial(ProductionPickBean productionPickBean) {
        /**
         * @description: 补料申请
         * @return:  * @param productionPickBean
         * @author:
         * @time: 2022/4/21 21:13
         */

        AssertUtil.isTrue(StringUtils.isBlank(productionPickBean.getProjectNo()), "项目编号为空");
        ProjectBean projectBean = projectBeanMapper.selectProjectByStatusAndProjectNo(productionPickBean.getProjectNo());
        AssertUtil.isTrue(projectBean == null, "项目号错误项目不存在");
        AssertUtil.isTrue(projectBean.getStatus() == 3 || projectBean.getStatus() == 2, "项目阶段不可领料");
        AssertUtil.isTrue(StringUtils.isBlank(productionPickBean.getMaterialNo()), "物料编码为空");
        MaterialBean materialBean = materialBeanMapper.selectByMaterialNo(productionPickBean.getMaterialNo());
        AssertUtil.isTrue(materialBean == null, "物料不存在");
        List<String> materialNos = bomBeanMapper.selectBomMaterialNoByAddSup(projectBean.getProjectNo());
        AssertUtil.isTrue(!materialNos.contains(productionPickBean.getMaterialNo()), "物料编码不属于该项目");
        AssertUtil.isTrue(productionPickBean.getMaterialCount() <= 0, "数量不合格");
        AssertUtil.isTrue(StringUtils.isBlank(productionPickBean.getPickDescribe()), "声明不能为空");
        productionPickBean.setPickType(1);
        productionPickBean.setStatus(0);
        productionPickBean.setProjectId(projectBean.getId());
        productionPickBean.setMaterialId(materialBean.getId());
        Integer integer = pickBeanMapper.insertSelective(productionPickBean);
        AssertUtil.isTrue(integer == null || integer == 0, "添加补料单失败");

    }


    public void updateAssignSupp(Integer id) {
        /**
         * @description: 审批补料单
         * @return:  * @param id
         * @author:
         * @time: 2022/4/21 22:05
         */
        AssertUtil.isTrue(id == null || id == 0, "审批失败");
        ProductionPickBean productionPickBean = pickBeanMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(productionPickBean == null, "审批失败 领料单不存在");
        productionPickBean.setPickType(0);
        Integer integer = pickBeanMapper.updateByPrimaryKeySelective(productionPickBean);
        AssertUtil.isTrue(integer == null || integer == 0, "审批失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    public void toQuality(String projectNo) {
        System.out.println(projectNo);
        AssertUtil.isTrue(StringUtils.isBlank(projectNo), "待更新记录不存在");
        ProjectBean bean = projectBeanMapper.selectProjectByProjectNo(projectNo);
        AssertUtil.isTrue(bean == null, "待更新记录不存在");
        bean.setStatus(2);
        Integer integer = projectBeanMapper.updateByPrimaryKeySelective(bean);
        AssertUtil.isTrue(integer == null || integer == 0, "更新失败");
        ProjectQualityBean qualityBean = new ProjectQualityBean();
        qualityBean.setProjectId(bean.getId());
        qualityBean.setQualityCreateDate(new Date());
        qualityBean.setQualityType(3);
        qualityBean.setQualityStatus(0);
        qualityBean.setQualityUpdateDate(new Date());
        Integer integer1 = qualityBeanMapper.insertSelective(qualityBean);
        AssertUtil.isTrue(integer1 == null || integer1 == 0, "更新失败");
    }
}
